<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

/**
 * @author: yfl
 * @QQ: 554665488
 * @description: 职务控制器
 * @time:2017-7-20 11:39:58
 * Class DutyController
 * @package app\admin\controller
 */
class DutyController extends BaseController
{
    protected $tableModel='Duty'; //表模型
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $list = model($this->tableModel)->getAllList();
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return $this->fetch();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->post();
            $res = model($this->tableModel)->add($data);
            if ($res) {
                return self::showReturnCodeWithOutData(1, '添加职务成功');
            } else {
                return self::showReturnCodeWithOutData(-1, '添加职务失败');
            }
        } else {
            return self::showReturnCodeWithOutData(404);
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        if ($this->request->isGet()) {
            $data = model($this->tableModel)->findById($id);
            if (!empty($data)) {
                $this->assign('data', $data);
            } else {
                return self::showReturnCodeWithOutData(-1, '数据为空');
            }
            return $this->fetch();
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        if($request->isPut()){
            $data=$request->put();
            $res=model($this->tableModel)->updateById($data,$id);
            if($res){
                return self::showReturnCodeWithOutData(1, '更新成功');
            }else{
                return self::showReturnCodeWithOutData(-1, '更新失败');
            }
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if ($this->request->isDelete()) {
            $res = model($this->tableModel)->delById($id);
            if ($res) {
                return self::showReturnCodeWithOutData(1, '删除成功');
            } else {
                return self::showReturnCodeWithOutData(-1, '删除失败');
            }
        }
    }
}
